package cqrtplm.historicaldata;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.pdm.form.model.dto.FormDataDTO;
import com.hustcad.plm.pdm.form.model.dto.FormDataFileMarkDTO;
import com.hustcad.plm.pdm.form.service.TyFormDataExtService;
import com.hustcad.plm.pdm.formusage.model.dto.FormFileDTO;
import com.ty.basic.entity.form.FormDataDO;
import cqrtplm.entity.ZdDbWithFormoid;
import cqrtplm.service.RTSpecifiedDataService;
import cqrtplm.util.FormDataGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/historicaldata/zddb")
public class ZdDbController {
    @Resource
    private final RTSpecifiedDataService rtSpecifiedDataService;
    @Resource
    private final TyFormDataExtService tyFormDataExtService;

    public ZdDbController(RTSpecifiedDataService zdDbService, TyFormDataExtService tyFormDataExtService
    ) {
        this.rtSpecifiedDataService = zdDbService;
        this.tyFormDataExtService = tyFormDataExtService;
    }

    @GetMapping("/null-formoid")
    public List<ZdDbWithFormoid> getZdDbWithNullFormoid() {
        List<ZdDbWithFormoid> zdDbWithNullFormoid = rtSpecifiedDataService.getZdDbWithNullFormoid();//物料集合
        System.out.println("zdDbWithNullFormoid = " + zdDbWithNullFormoid);
        zdDbWithNullFormoid.stream().forEach(zdDbWithFormoid -> {
            String aid = zdDbWithFormoid.getAid() ;
            List<MultipartFile> fileList = new ArrayList<>();  //附件
            FormDataDTO formDataDTO = new FormDataDTO();
            // 3. 生成表单JSON字符串
            String formData = FormDataGenerator.generateFormData(zdDbWithFormoid);
            formDataDTO.setFormDataStr(formData);// 表单中内容赋值
            formDataDTO.setFormtemplateotype("ty.inteplm.form.CTyFormTemplate");
            formDataDTO.setFormtemplateoid(new BigInteger("751426729789177856"));
            formDataDTO.setContaineroid(new BigInteger("747656141957578752"));
            formDataDTO.setContainerotype("ty.inteplm.product.CTyPDMProduct");
            formDataDTO.setDomainoid(new BigInteger("747656180624867328"));
            formDataDTO.setDomainotype("ty.inteplm.domain.CTyDomain");
            formDataDTO.setCabinetotype("ty.inteplm.folder.CTyCabinet");
            formDataDTO.setSubfolderoid(new BigInteger("747656180624867328"));
            formDataDTO.setSubfolderotype("ty.inteplm.folder.CTySubFolder");
            formDataDTO.setLifecyclestageotype("ty.inteplm.lifecyclestage.CTyLifecycleStage");
            formDataDTO.setLifecycletemplateotype("ty.inteplm.lifecyclestage.CTyLifecycleTemplate");
            formDataDTO.setTeamotype("ty.inteplm.team.CTyTeam");
            formDataDTO.setTeamtemplateotype("ty.inteplm.team.CTyTeamTemplate");
            formDataDTO.setObjectNumber(aid);//物料编码
            formDataDTO.setTypeotype("ty.inteplm.type.CTyTypeDef");
            formDataDTO.setCreatorotype("ty.inteplm.user.CTyUser");
            formDataDTO.setUpdatorotype("ty.inteplm.user.CTyUser");
            formDataDTO.setOwnerotype("ty.inteplm.user.CTyUser");
            formDataDTO.setOwneroid(new BigInteger("105"));//**
            formDataDTO.setClassficationotype("ty.inteplm.classification.CTyClassification");
            formDataDTO.setClassificationotype("ty.inteplm.classification.CTyClassification");
            formDataDTO.setTypeoid(new BigInteger("747438986741579776"));
            formDataDTO.setName(aid);//物料名称
            List<FormDataFileMarkDTO> formDataFileMarkDTOList = new ArrayList<>();
            List<BigInteger> addFileList = new ArrayList<>();
            List<BigInteger> removeFileList = new ArrayList<>();
            FormFileDTO formFileDTO = new FormFileDTO();
            formFileDTO.setAddFileList(addFileList);
            formFileDTO.setRemoveFileList(removeFileList);
            FormDataDO formDataDO = tyFormDataExtService.createFormDataWithFile(formDataDTO, formDataFileMarkDTOList,
                                                                                fileList, formFileDTO);
            System.out.println("formDataDO = " + formDataDO);
            if (StringUtils.isNotBlank(zdDbWithFormoid.getDetailId())){
                rtSpecifiedDataService.updateMxbFormId(zdDbWithFormoid.getDetailId(), formDataDO.getOid());
            }
        });

        return rtSpecifiedDataService.getZdDbWithNullFormoid();
    }
}